import config from "./WebIMConfig";
import websdk from "easemob-websdk";

// import emedia from "easemob-emedia";
// import webrtc from "easemob-webrtc";
// import webrtc from "./EMedia_x1v1_3.4.1";
// 声网音视频sdk
// import AgoraRTC from "agora-rtc-sdk-ng"

function ack(message) {
	var bodyId = message.id; // 需要发送已读回执的消息id
	var msg = new WebIM.message("read", WebIM.conn.getUniqueId());
	msg.set({
		id: bodyId,
		to: message.from
	});
	WebIM.conn.send(msg.body);
}

const rtc = {
	// 用来放置本地客户端。
	client: null,
	// 用来放置本地音视频频轨道对象。
	localAudioTrack: null,
	localVideoTrack: null,
};

// 初始化IM SDK
var WebIM = {};
WebIM = window.WebIM = websdk;
WebIM.config = config;
WebIM.conn = new WebIM.connection({
	appKey: WebIM.config.appkey,
	isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
	https: WebIM.config.https,
	isAutoLogin: true,
	heartBeatWait: WebIM.config.heartBeatWait,
	autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
	autoReconnectInterval: WebIM.config.autoReconnectInterval,
	isStropheLog: WebIM.config.isStropheLog,
	delivery: WebIM.config.delivery,

	//公有云 isHttpDNS 默认配置为true
	isHttpDNS: WebIM.config.isHttpDNS,

	// 私有云设置，详细文档：http://docs-im.easemob.com/im/web/other/privatedeploy
	// isHttpDNS: false,
	// url: 'https://im-api-v2.easecdn.com/ws', // 设置为私有云的websocket server url
	// apiUrl: 'https://a1.easecdn.com', // 设置为私有云的rest server url
});

//通话状态
const CALLSTATUS = {
	idle: 0,
	inviting: 1,
	alerting: 2,
	confirmRing: 3, // caller
	receivedConfirmRing: 4, // callee
	answerCall: 5,
	receivedAnswerCall: 6,
	confirmCallee: 7
}

// 注册监听回调
WebIM.conn.listen({
  // 连接回调
	onOpened: function (message) { // 连接成功回调
	  // console.log('message onOpened平常是否收到消息？', message)
    Vue.$router.push({ path: '/contact' }); // 登录或注册成功后 跳转到好友页面
	},

  // 关闭回调
  onClosed: function (message) {

    console.log('是否关闭 onClosed')
    Vue.$router.push({ path: "/" });
  },


  // 收到文本消息
	onTextMessage: function (message) {
		const { from, to, type, time } = message;
		const chatId = type !== "chat" ? to : from;

		Vue.$store.commit("updateMsgList", {
			chatType: 'contact',
			chatId: chatId,
			msg: message.data,
			bySelf: false,
			from: message.from,
			mid: message.id,
			time: time
		});
	},


  // 收到表情消息
	onEmojiMessage: function (message) {
		// console.log("收到表情消息", message);
		// const { type } = message;
		// type === 'chat' && ack(message);
	},

  // 收到图片消息
	onPictureMessage: function (message) {
		const { from, to, type, time } = message;
		const chatId = type !== "chat" ? to : from;

		Vue.$store.commit("updateMsgList", {
			chatType: 'contact',
			chatId: chatId,
			msg: message.url,
			bySelf: false,
			type: "img",
			from: message.from,
			time: time
		});
		type === 'chat' && ack(message);
	},

  // 收到命令消息
	onCmdMessage: function (msg) {
		// console.log('收到命令消息', msg)
	},


   // 收到文件消息
	onFileMessage: function (message) {
	  // console.log('收到文件消息', message)
		const { from, to, type, time } = message;
		const chatId = type !== "chat" ? to : from;

		Vue.$store.commit("updateMsgList", {
			chatType: 'contact',
			chatId: chatId,
			msg: message.url,
			bySelf: false,
			type: "file",
			filename: message.filename,
			file_length: message.file_length,
			from: message.from,
			time: time
		});
		type === 'chat' && ack(message);
	},

  // 好友列表，获取好友列表时触发
	onRoster: function (message) {
	  // TODO 后台增加好友，是否触发这个？
		// console.log("获取好友列表时触发， 增加好友列表是否触发这个接口？", message);
	},

  // 收到消息送达服务器回执，发送消息时触发，文本、文件、图片
	onReceivedMessage: function (message) {
		// console.log("收到消息送达服务器回执 发送消息时触发，文本、文件、图片", message);
		// Vue.$store.commit("updateMessageMid", message);
		// message.status = "sent";
		// Vue.$store.commit("updateMessageStatus", message);
	},

  // 收到消息送达客户端回执
	onDeliveredMessage: function (message) {
		// console.log("收到消息送达客户端回执", message);
		// Vue.$store.commit('updateMessageStatus', message)
	},

  // 收到消息已读回执，进入聊天页面，触发这个，可将消息变成已读
	onReadMessage: function (message) {
		// console.log("收到消息已读回执, 进入聊天页面，触发这个，发送消息也触发", message);
		// message.status = "read";
		// Vue.$store.commit("setUnReadMsgList", message);
	},

});


// WebIM.WebRTC = window.webrtc; // 本项目引入 UMD 文件有问题，暂时这样引入
WebIM.EMedia = window.emedia;
// WebIM.AgoraRTC = AgoraRTC;
WebIM.rtc = rtc;

export default WebIM;
